--利用lua匹配字符串匹配目标hack跳转
--[[
objdump -d hack.elf

]]
local objdump_path = "%objdump%"
local elf = "hack.elf";
local format = string.format;
local f = io.popen(string.format("%s -d %s",objdump_path,elf));
assert(f);
local s = f:read("*a");
--print(s);
local out = io.stdout;
out = io.open(arg[1],"w+");
out:write("JMPS := \\\n");
local list = {};
for target,addr,reg in string.gmatch(s,"(%x+)%s%<JMP(%x+)_(r%d+)%>%:")do
    --print(target,addr,reg);
    local jmps = [[
    .thumb
    ldr %s,=0x%s+1
    bx %s

]]
    jmps = format(jmps,reg,target,reg);
    local name = format('  %s.s',addr);
    local file = io.open(format("jmp/%s.s",addr),"w+");
    file:write(jmps);
    --print(jmps);
    out:write(name,'\\\n');
    table.insert(list,addr);
end
out:write('\n');
out:write('build_jmp:\n');

for k,addr in ipairs(list)do
    out:write(format([[
%s$(CC) -c jmp/%s.s -o jmp/%s.o
%s$(OBJCOPY) jmp/%s.o -O binary jmp/%s.bin
%slua write.lua 0x%s jmp/%s.bin $(ROM)
]],
"\t",addr,addr,
"\t",addr,addr,
"\t",
addr,
addr
)

)
end

